﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CLibArtreve.dal;
using System.Configuration;
using System.Data;
using CLibArtreve.models;

namespace CLibArtreve.bll
{
    public class UserAdressBLL
    {
        #region VARIABLES
        private string _connectionString;
        private UserAdressDAL _adapter;
        #endregion

        #region PROPERTIES
        public string ConnectionString
        {
            get {
                if (_connectionString == null)
                    _connectionString = ConfigurationManager.ConnectionStrings["ARTREVEConnectionString"].ConnectionString;
                return _connectionString; 
            }
            set { _connectionString = value; }
        }
        public UserAdressDAL Adapter
        {
            get
            {
                if (_adapter == null)
                    _adapter = new UserAdressDAL(ConnectionString);
                return _adapter;
            }
        }
        #endregion

        #region CONSTRUCTOR
        public UserAdressBLL(string connectionString)
        {
            ConnectionString = connectionString;
        }
        #endregion

        #region OVERRIDE METHODS
        public override string ToString()
        {
            return "BUSINESS LOGIC LAYER: " + this.GetType().ToString();
        }
        #endregion

        #region GET METHODS
        public IList<UserAdress> GetAllUserAdresses()
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetAllUserAdresses();
                //MAKE A LIST
                IList<UserAdress> list = new List<UserAdress>();
                //LOOP THROUGH ROWS OF DATASET
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(GenerateArtFromDataRow(row));
                }
                //RETURN LIST
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public UserAdress GetAdressesByUserId(Int64? id)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetAdressesByUserId(id);

                //IF DATASET EXISTS EN IT HAS ROWS RETURN THE ROW
                if(ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    return GenerateArtFromDataRow(row);
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public UserAdress GetUsersByAdresseId(Int64? id)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetUsersByAdresseId(id);

                //IF DATASET EXISTS EN IT HAS ROWS RETURN THE ROW
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    return GenerateArtFromDataRow(row);
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
       
        #endregion

        #region UTILITIES
        private UserAdress GenerateArtFromDataRow(DataRow row)
        {
            UserAdress obj = new UserAdress();
            obj.Id = (Int64)row["address_id"];
            obj.UserId = (Int64)row["user_id"];
            obj.Number = (Int64)row["address_number"];
            obj.Street = (string)row["address_street"];
            obj.Zipcode = (Int64)row["address_zipcode"];
            obj.City = (string)row["address_city"];
            obj.State = (string)row["address_state"];
            obj.CountryIso = (string)row["address_country_iso"];
            
            return obj;
        }
        #endregion
    }
}
